package com.redball;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

public class MainActivity extends Activity implements SensorEventListener
{

	private SensorManager mSensorManager;
	private Sensor mAccelerometer;
	private Panel _panel;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		_panel = new Panel(this);
		setContentView(_panel);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu)
	{
		super.onMenuOpened(featureId, menu);

		MenuItem item = menu.findItem(R.id.menu_stop);

		if (_panel.isPaused())
		{
			item.setTitle("Resume...");
		} else
		{
			item.setTitle("Pause");
		}

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId()) {
		case R.id.menu_stop:
			_panel.togglePause();
			return true;
		case R.id.menu_settings:
			showAlert("Settings", "Not implemented yet..");
			return true;
		case R.id.menu_gravity:
			showAlert("Change Gravity", "This feature is disabled in this version..");
			// _panel.changeGravity();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void showAlert(String title, String text)
	{
		AlertDialog alert = new AlertDialog.Builder(this).create();
		alert.setMessage(title);
		alert.setMessage(text);
		alert.setButton(DialogInterface.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
			}
		});
		alert.show();
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		mSensorManager.unregisterListener(this);
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy)
	{
		// TODO Auto-generated method stub

	}

	private int a = 100;// testing use

	public void onSensorChanged(SensorEvent event)
	{
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
		{
			_panel.setGravityX(-event.values[0]);
			_panel.setGravityY(event.values[1]);
			// testing use
			if (a == 0)
			{
				System.out.println("X: " + event.values[0]);
				System.out.println("Y: " + event.values[1]);
				System.out.println("Z: " + event.values[2]);
				a=5;
			} else
			{
				--a;
			}
		}
	}
}
